package html;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspWriter;

import util.MyLogger;
import db.DBObject;

public class RequestParser extends MyLogger {

	protected HttpServletRequest request = null;
	public static String send_action_file = null, send_method = null;
	protected String content = null, submit = null, delete = null, add=null;
	protected String[] input = null, insert_columns = null, display_columns = null,fk_columns=null;
	protected String pk_column = null;
	protected DBObject table = null;
	protected Boolean admin=false;

	protected ArrayList<ArrayList<Object>> array = null;
	protected Long pk_id = null;
	protected Long[] fk_ids=null;

	public RequestParser(HttpServletRequest request, String send_action,
			String send_method) {
		this.request = request;
		RequestParser.send_action_file = send_action;
		RequestParser.send_method = send_method;
	}

	public String getAdd() {
		return add;
	}

	public void setAdd(String add) {
		this.add = add;
	}

	public void isAdmin(boolean admin) {
		this.admin = admin;
	}

	private boolean areFieldsNotNull() {
		return (request != null && send_action_file != null
				&& send_method != null && content != null && submit != null
				&& delete != null && input != null && insert_columns != null
				&& display_columns != null && pk_column != null && table != null);
	}

	public void process() {
		if (areFieldsNotNull()) {
			pk_id = RequestParser.getLongFromParameter(request, content);

			if(admin) {
				if (request.getParameter(submit) != null) {
					if (RequestParser.areAllParametersNotNull(request, input)) {
						String[] result = RequestParser.getStringArrayFromParameters(
								request, input);
						pk_id = table.insertGetPK(insert_columns, result);
					} else if (RequestParser.areSomeParametersNotNull(request, input)) {
						// TODO Error output because only one parameter is set -
						// needed are both!
					}
				}
			}

			// #########################################################
			// FIXME add additional functions so it is i.e. possible to delete
			// only all related towns or vice versa
			// #########################################################
			if(admin) {
				if (request.getParameter(delete) != null && pk_id != null) {
					table.delete(pk_column, pk_id);
				}
			}

			array = table.getSelect(display_columns, fk_columns, fk_ids);
		}
	}

	public String getSend_action_file() {
		return send_action_file;
	}

	public void setSend_action_file(String send_action_file) {
		RequestParser.send_action_file = send_action_file;
	}

	public String getSend_method() {
		return send_method;
	}

	public void setSend_method(String send_method) {
		RequestParser.send_method = send_method;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getSubmit() {
		return submit;
	}

	public void setSubmit(String submit) {
		this.submit = submit;
	}

	public String getDelete() {
		return delete;
	}

	public void setDelete(String delete) {
		this.delete = delete;
	}

	public String[] getInput() {
		return input;
	}

	public void setInput(String[] input) {
		this.input = input;
	}

	public String[] getInsert_columns() {
		return insert_columns;
	}

	public void setInsert_columns(String[] insert_columns) {
		this.insert_columns = insert_columns;
	}

	public String[] getDisplay_columns() {
		return display_columns;
	}

	public void setDisplay_columns(String[] display_columns) {
		this.display_columns = display_columns;
	}

	public String getPk_column() {
		return pk_column;
	}

	public void setPk_column(String pk_column) {
		this.pk_column = pk_column;
	}

	public DBObject getTable() {
		return table;
	}

	public void setTable(DBObject table) {
		this.table = table;
	}
	
	public void setArray(ArrayList<ArrayList<Object>> array){
		this.array=array;
	}

	public ArrayList<ArrayList<Object>> getArray() {
		return array;
	}

	public Long getPk_id() {
		return pk_id;
	}

	public void setPk_id(Long pk_id) {
		this.pk_id = pk_id;
	}

	public static void printOptionList(JspWriter out, ArrayList<ArrayList<Object>> array,Object match) throws IOException{
		String p=null;
		p="<option > </option>";
		out.print(p);
		for (int i=1;i<array.size();i++) {
			ArrayList<Object> key=array.get(i);
			p="<option"+(key.get(0).equals(match)?" selected":"")
			+" value=\""+key.get(0)+"\" >"+key.get(1)+(key.size()>=3?" ("+key.get(2)+") ":"") +"</option>";
			out.println(p);
		}
	}
	
	public void printOptionList(JspWriter out) throws IOException{
		String p=null;
		p="<option > </option>";
		out.print(p);
		for (int i=1;i<array.size();i++) {
			ArrayList<Object> key=array.get(i);
			p="<option"+(key.get(0).equals(pk_id)?" selected":"")
			+" value=\""+key.get(0)+"\" >"+key.get(1)+(key.size()>=3?" ("+key.get(2)+") ":"") +"</option>";
			out.println(p);
		}
	}

	public static Long getLongFromParameter(HttpServletRequest request, String parameter){
		Long result=null;
		String p=request.getParameter(parameter);
		if(p!=null&&p!=""&&!p.equalsIgnoreCase("null")){
			try{
				result=Long.parseLong(p);
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		return result;
	}

	public static Integer getIntegerFromParameter(HttpServletRequest request, String parameter){
		Integer result=null;
		if(request.getParameter(parameter)!=null){
			try{
				result=Integer.parseInt(request.getParameter(parameter));
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		return result;
	}

	public static boolean areAllParametersNotNull(HttpServletRequest request, String[] input){
		boolean notNull=true;
		for(String s:input)
			if(request.getParameter(s)==null)notNull=false;
		return notNull;
	}

	public static boolean areSomeParametersNotNull(HttpServletRequest request, String[] input){
		boolean notNull=false;
		for(String s:input)
			if(request.getParameter(s)!=null)notNull=true;
		return notNull;
	}

	public static String[] getStringArrayFromParameters(HttpServletRequest request, String[] input){
		String[] result=new String[input.length];
		for(int i=0;i<input.length;i++){
			result[i]=request.getParameter(input[i]);
		}
		return result;
	}

	public String getFk_column() {
		return fk_columns[0];
	}
	
	public String[] getFk_columns() {
		return fk_columns;
	}

	public void setFk_column(String fk_column) {
		String[] temp =new String[fk_columns!=null?(fk_columns.length+1):1];
		for(int i=1;i<temp.length;i++){
			temp[i]=fk_columns[i];
		}
		temp[0]=fk_column;
		this.fk_columns = temp;
	}
	
	public void setFk_columns(String[] fk_column) {
		this.fk_columns = fk_column;
	}

	public Long getFk_id() {
		return fk_ids[0];
	}

	public Long[] getFk_ids() {
		return fk_ids;
	}

	
	public void setFk_id(Long fk_id) {
		Long[] temp =new Long[fk_ids!=null?(fk_ids.length+1):1];
		for(int i=1;i<temp.length;i++){
			temp[i]=fk_ids[i];
		}
		temp[0]=fk_id;
		this.fk_ids = temp;
	}

	public void setFk_ids(Long[] fk_id) {
		this.fk_ids = fk_id;
	}
}
